package laborhazi;

//import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
//import java.awt.image.BufferedImage;
//import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;

public class LemmingAnim extends Animation {
	private Viewer viewer;
	private Image image;
	private ImageIcon im;
	public LemmingAnim(Viewer v) {
		viewer = v;
		im = new ImageIcon("lemming.gif");
	}
	public void animate(Lemming lemming, Graphics g, int phase) {
		image = im.getImage();
		int[] position;
		int[] posField;
		int[] oldPosField;
		/* initialize variables */
		position = new int[2];
		posField = new int[2];
		oldPosField = new int[2];
		/*getting positions for calculation */
		position = viewer.fieldToView(lemming.getPosition());
		posField = lemming.getPosition();
		oldPosField = lemming.getOldPos();
		/* calculate next position */
		if (oldPosField[0] == posField[0]) {
			if (oldPosField[1] != posField[1]) {
				position[1] = position[1]-20+phase;
			} 
		} else if (oldPosField[0] < posField[0]) {
			if (oldPosField[1] == posField[1]) {
				position[0] =position[0]-20+phase;
			} else {
				position[0] =position[0]-20+phase;
				position[1] =position[1]+20-phase;
			}
		} else if (oldPosField[0] > posField[0]) {
			if (oldPosField[1] == posField[1]) {
				position[0] =position[0]+20-phase;
			} else {
				position[0] =position[0]+20-phase;
				position[1] =position[1]+20-phase;
			}
		}
		/* check tools in use and directions */
		
		/* building */
		if (lemming.isErecting()) {
			Image erectIm;
			ImageIcon imageIc;
			
			/* set direction */
			if (lemming.getDir() == 1) {
				imageIc = new ImageIcon("build_to_right.gif");
			} else imageIc = new ImageIcon("build_to_left.gif");
			erectIm = imageIc.getImage();
			g.drawImage(erectIm, position[0], position[1], 18, 20, viewer);
			}
		else
			/* drilling */
			if (lemming.isDrilling()) {
				Image drillIm;
				ImageIcon imageIc;
				
				/* set direction */
				if (lemming.getDir() == 1) {
					imageIc = new ImageIcon("drill_to_right.gif");
				} else imageIc = new ImageIcon("drill_to_left.gif");
				drillIm = imageIc.getImage();
				g.drawImage(drillIm, position[0], position[1] + 1, 20, 19, viewer);
				} 
			else 
				/* parachuting */
				if (lemming.isParachuting()) {
					Image parachuteIm;
					ImageIcon imageIc;
					
					imageIc = new ImageIcon("parachute.gif");
					parachuteIm = imageIc.getImage();
					g.drawImage(parachuteIm, position[0], position[1] + 4, 19, 20, viewer);
					} 
				else 
				/* barricade */
				if (lemming.getDir() == -1) {
					Image barricadeIm;
					ImageIcon imageIc;
					
					imageIc = new ImageIcon("barricade.gif");
					barricadeIm = imageIc.getImage();
					g.drawImage(barricadeIm, position[0], position[1] + 4, 19, 16, viewer);
					} 
				else {
				/* walking */
					Image walkIm;
					ImageIcon imageIc;
					
					/* set direction */
					if (lemming.getDir() == 1) 
						imageIc = new ImageIcon("walk_to_right.gif");	
					 else 
						imageIc = new ImageIcon("walk_to_left.gif");
					walkIm = imageIc.getImage();
					g.drawImage(walkIm, position[0], position[1] + 4, 14, 16, viewer);
				}
		if (lemming.isExploding()) {
			Image explodeIm;
			ImageIcon imageIc;
			imageIc = new ImageIcon("expl_sign.gif");
			
			explodeIm = imageIc.getImage();
			g.drawImage(explodeIm, position[0], position[1], 10, 10, viewer);
		}

//		If the lemming is exploding we desplay a counter
		/*if (lemming.isExploding()) {
			Integer time;
			time = new Integer(lemming.getExplodeCounter());
			g.setColor(Color.red);
			g.drawString(time.toString(), position[0]+5, position[1]+8);
		}*/
	}
	private void stepRigh() {
		
	}
	private void stepRighUp() {
		
	}
	private void stepLeft() {
		
	}
	private void stepLeftUp() {
		
	}
}
